package com.youcan.controller.modelcontroller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youcan.entity.Recommend;
import com.youcan.service.modelservices.RecommendService;
import com.youcan.util.Constants;

/**
 * @author time E-mail:timezone85@gmail.com
 * @date:2012-8-19 上午11:07:06
 * @version :V1.0
 *
 */

@Component
@Controller
public class RecommendController {
	protected final Log logger = LogFactory.getLog(RecommendController.class);

	@Autowired
	@Qualifier("recommendServiceImpl")
	private RecommendService recommendService;

	//通过注解进行转发控制 POST来的请求并把参数绑定
	@ResponseBody
	@RequestMapping(value = "/AddRecommend", method = RequestMethod.POST)
	public JSONObject AddRecommend(
			@RequestParam("goodsId") String goodsId,
			@RequestParam("storeId") String storeId,//商家？
			@RequestParam("level") Integer level,
			@RequestParam("sort") Integer sort,
			@RequestParam("recType") String recType,
			@RequestParam("recDays") Integer recDays,
			@RequestParam("startDate") Date startDate,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
		
		Recommend recommend = new Recommend();
		JSONObject json = new JSONObject();
		try {
			recommend.setGoodsId(goodsId);
			recommend.setStoreId(storeId);
			recommend.setLevel(level);
			recommend.setSort(sort);
			recommend.setRecType(recType);
			recommend.setRecDays(recDays);
			recommend.setStartDate(startDate);
			recommendService.addEntity(recommend);
			json.put(Constants.RES_CODE, Constants.CODE_SUCCESS);
			json.put(Constants.RES_MESSAGE, "推荐成功。");
		} catch (Exception e) {
			json.put(Constants.RES_CODE, Constants.CODE_FAILED);
			json.put(Constants.RES_MESSAGE, "推荐失败。");
		}

		return json;
	}
}
